//
//  LXSearchCarView.m
//  INMOTION_CustomMadeProject
//
//  Created by cjg on 2021/3/24.
//  Copyright © 2021 cjg. All rights reserved.
//

#import "LXSearchCarView.h"
#import "IMTConfig.h"
#import "IMTCANManager.h"
#import "IMTCarManager.h"
#import "NSObject+HUD.h"
#import "KSLanguageManager.h"

@interface LXSearchCarView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *alertTitle;
@property (nonatomic, strong) NSArray *dataList;

@end

@implementation LXSearchCarView
- (instancetype)init {
    
    self = [[[NSBundle mainBundle] loadNibNamed:@"LXSearchCarView" owner:self options:nil] lastObject];
    
    [self.alertTitle setTitle:IMTNewCarLocalizedString(@"dialog_scan_devices_title") forState:(UIControlStateNormal)];
//    self.titleLB.text = IMTNewCarLocalizedString(@"dialog_scan_devices_title");
    [self.searchBtn setTitle:IMTNewCarLocalizedString(@"search_again") forState:(UIControlStateNormal)];
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.backgroundColor = IMTColorAlpha(30, 30, 30, 0.5);
    self.tableView.separatorColor = IMTColor(230, 230, 230);
    self.tableView.separatorInset = UIEdgeInsetsZero;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss:nil];
}

- (void)showdataList:(NSArray *)dataList {
    self.dataList = dataList;
    [self.tableView reloadData];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self] ;
    
    self.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    } completion:nil];
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
    } completion:nil];
    self.isShowing = true;
}

- (void)dismiss:(void(^)(void))completion {
    self.alpha = 1;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (completion) {
            completion();
        }
        self.isShowing = false;
    }];
}

// 搜索车辆
- (IBAction)searchCarAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(carListViewScan)]) {
        [self.delegate carListViewScan];
        [self dismiss:nil];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = IMTColor(68, 68, 68);
    CBPeripheral *device = self.dataList[indexPath.row];
//    cell.textLabel.text = device.RSSI.stringValue;
    cell.textLabel.text = @"OYDS";
    cell.detailTextLabel.text = device.name;
//    cell.imageView.image = [UIImage imageNamed:@"car_route_list"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CBPeripheral *device = self.dataList[indexPath.row];
    NSLog(@"==================");

    if (self.delegate && [self.delegate respondsToSelector:@selector(carListViewDidSelectInfo:)]) {
        NSLog(@"==================");
        [self.delegate carListViewDidSelectInfo:device];
    }
    [self dismiss:nil];
}

@end
